﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Templates_Home_Default : PageBase
{
    protected void Page_Load(object sender, EventArgs e)
    {
        dsAudits.Select += new EventHandler<Acms.DataControls.EventDrivenDataSourceSelectEventArgs>(dsAudits_Select);
        dsCommitments.Select += new EventHandler<Acms.DataControls.EventDrivenDataSourceSelectEventArgs>(dsCommitments_Select);
        dsNewUsers.Select += new EventHandler<Acms.DataControls.EventDrivenDataSourceSelectEventArgs>(dsNewUsers_Select);
        dsMerit.Select += new EventHandler<Acms.DataControls.EventDrivenDataSourceSelectEventArgs>(dsMerit_Select);
        if (!IsPostBack)
        {
            dsProcesses.SiteId = dsUserTips.SiteId = AcmsContext.SiteId;
            dsProcesses.SiteVersion = dsUserTips.SiteVersion = AcmsContext.SiteVersion;
            DataBind();
        }
    }

    void dsAudits_Select(object sender, Acms.DataControls.EventDrivenDataSourceSelectEventArgs e)
    {
        var factory = new CarbonExchange.Bll.AuditFactory();
        e.SetData(factory.GetAllByUserId(User.Identity.User.Id).ToList());
        
    }

    void dsMerit_Select(object sender, Acms.DataControls.EventDrivenDataSourceSelectEventArgs e)
    {
        var factory = new CarbonExchange.Bll.FootprintSavingFactory();
        e.SetData(factory.Get(null, AcmsContext.SiteId, 0, 5));
    }

    void dsNewUsers_Select(object sender, Acms.DataControls.EventDrivenDataSourceSelectEventArgs e)
    {

        e.SetData(Acms.Bll.User.GetMostRecent(AcmsContext.SiteId, 5));
    }

    void dsCommitments_Select(object sender, Acms.DataControls.EventDrivenDataSourceSelectEventArgs e)
    {
        if(Request.IsAuthenticated)
        {
            var factory = new CarbonExchange.Bll.CommitmentFactory();
            var data = factory.GetCommitmentDatesByUserId(User.Identity.User.Id).Take(5);
            e.SetData(data);
        }
    }

    private class HomePageData
    {
        internal int totalCarbonAudited { get; set; }
        internal int totalCarbonPledged { get; set; }
        internal int totalUsers { get; set; }
        internal int totalCarbonSaved { get; set; }
    }

    public override void DataBind()
    {
        HomePageData homePageData = null;
        if (Cache["HomePageData"] != null && Cache["HomePageData"] is HomePageData)
        {
            homePageData = (HomePageData)Cache["HomePageData"];
        }
        if(homePageData == null)
        {
            homePageData = new HomePageData();
            var commitmentFactory = new CarbonExchange.Bll.CommitmentFactory();
            var auditFactory = new CarbonExchange.Bll.AuditFactory();
            homePageData.totalCarbonAudited = auditFactory.GetTotalCarbonAudited();
            homePageData.totalCarbonPledged = commitmentFactory.GetTotalCarbonPledged();
            homePageData.totalUsers = Acms.Bll.User.GetCount(AcmsContext.SiteId);
            Cache.Add("HomePageData", homePageData, null, DateTime.Now.AddMinutes(30), TimeSpan.Zero, System.Web.Caching.CacheItemPriority.Normal, null);
        }
        string format = "#,###0";
        lblCarbonAudited.Text = homePageData.totalCarbonAudited.ToString(format);
        lblCarbonPledged.Text = homePageData.totalCarbonPledged.ToString(format);
        lblNoOfUsers.Text = homePageData.totalUsers.ToString(format);
        lblTotalCarbonSaved.Text = homePageData.totalCarbonSaved.ToString(format);
        base.DataBind();
    }
}